﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace dk.Helper.Base.Core
{
    public class AttacksPlanner : IAttacksPlanner
    {
        private const int UNIT_SPEED_RAM = 30;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.Object"/> class.
        /// </summary>
        public AttacksPlanner()
        {
            Attacks = new BindingList<IAttack>();
        }

        #region IAttacksPlanner Members

        /// <summary>
        /// Gets the attacks.
        /// </summary>
        public BindingList<IAttack> Attacks { get; private set; }

        /// <summary>
        /// Creates the attacks.
        /// </summary>
        /// <param name="from">From.</param>
        /// <param name="to">To.</param>
        public void CreateAttacks(IPlayer from, IPlayer to)
        {
            CreateAttacks(from.Villages, to.Villages);
        }

        /// <summary>
        /// Occurs when Attacks are changed.
        /// </summary>
        public event EventHandler AttacksChanged;

        /// <summary>
        /// Removes the attack.
        /// </summary>
        /// <param name="attack">The attack.</param>
        public void RemoveAttack(IAttack attack)
        {
            if (attack == null) return;
            if (Attacks.Contains(attack))
            {
                Attacks.Remove(attack);
            }
        }

        /// <summary>
        /// Creates the attacks with shortest average duration times.
        /// </summary>
        /// <param name="fromVillages">From.</param>
        /// <param name="toVillages">To.</param>
        public void CreateAttacks(IList<IVillage> fromVillages, IList<IVillage> toVillages)
        {
            IDictionary<KeyValuePair<IVillage, IVillage>, double> distancesFromTo =
                new Dictionary<KeyValuePair<IVillage, IVillage>, double>();
            foreach (IVillage fromVillage in fromVillages)
                foreach (IVillage toVillage in toVillages)
                {
                    double distance = fromVillage.GetDistanceTo(toVillage);
                    var pair = new KeyValuePair<IVillage, IVillage>(fromVillage, toVillage);
                    distancesFromTo.Add(pair, distance);
                }

            foreach (var pair in distancesFromTo.Keys)
            {
                IAttack attack = new Attack(pair.Key, pair.Value, UNIT_SPEED_RAM);
                attack.Changed += Attack_Changed;
                Attacks.Add(attack);
            }
            OnAttacksChanged(new EventArgs());
        }

        #endregion

        /// <summary>
        /// Raises the <see cref="AttacksChanged"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        public void OnAttacksChanged(EventArgs e)
        {
            if (AttacksChanged != null) AttacksChanged(this, e);
        }

        private void Attack_Changed(object sender, EventArgs e)
        {
            OnAttacksChanged(new EventArgs());
        }
    }
}